package com.ruoyi.wms.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.wms.domain.WmsInventoryCountDetail;
import com.ruoyi.wms.mapper.WmsInventoryCountMapper;
import com.ruoyi.wms.domain.WmsInventoryCount;
import com.ruoyi.wms.service.IWmsInventoryCountService;

/**
 * 盘点单Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-08
 */
@Service
public class WmsInventoryCountServiceImpl implements IWmsInventoryCountService 
{
    @Autowired
    private WmsInventoryCountMapper wmsInventoryCountMapper;

    /**
     * 查询盘点单
     * 
     * @param id 盘点单ID
     * @return 盘点单
     */
    @Override
    public WmsInventoryCount selectWmsInventoryCountById(Long id)
    {
        WmsInventoryCount wmsInventoryCount = wmsInventoryCountMapper.selectWmsInventoryCountById(id);
        return wmsInventoryCount;
    }

    /**
     * 查询盘点单列表
     * 
     * @param wmsInventoryCount 盘点单
     * @return 盘点单
     */
    @Override
    public List<WmsInventoryCount> selectWmsInventoryCountList(WmsInventoryCount wmsInventoryCount)
    {
        return wmsInventoryCountMapper.selectWmsInventoryCountList(wmsInventoryCount);
    }

    /**
     * 新增盘点单
     * 
     * @param wmsInventoryCount 盘点单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWmsInventoryCount(WmsInventoryCount wmsInventoryCount)
    {
        wmsInventoryCount.setCreateTime(DateUtils.getNowDate());
        int rows = wmsInventoryCountMapper.insertWmsInventoryCount(wmsInventoryCount);
        insertWmsInventoryCountDetail(wmsInventoryCount);
        return rows;
    }

    /**
     * 修改盘点单
     * 
     * @param wmsInventoryCount 盘点单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWmsInventoryCount(WmsInventoryCount wmsInventoryCount)
    {
        wmsInventoryCount.setUpdateTime(DateUtils.getNowDate());
        wmsInventoryCountMapper.deleteWmsInventoryCountDetailByInventoryCountId(wmsInventoryCount.getId());
        insertWmsInventoryCountDetail(wmsInventoryCount);
        return wmsInventoryCountMapper.updateWmsInventoryCount(wmsInventoryCount);
    }

    /**
     * 批量删除盘点单
     * 
     * @param ids 需要删除的盘点单ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsInventoryCountByIds(Long[] ids)
    {
        wmsInventoryCountMapper.deleteWmsInventoryCountDetailByInventoryCountIds(ids);
        return wmsInventoryCountMapper.deleteWmsInventoryCountByIds(ids);
    }

    /**
     * 删除盘点单信息
     * 
     * @param id 盘点单ID
     * @return 结果
     */
    @Override
    public int deleteWmsInventoryCountById(Long id)
    {
        wmsInventoryCountMapper.deleteWmsInventoryCountDetailByInventoryCountId(id);
        return wmsInventoryCountMapper.deleteWmsInventoryCountById(id);
    }

    /**
     * 新增盘点明细信息
     * 
     * @param wmsInventoryCount 盘点单对象
     */
    public void insertWmsInventoryCountDetail(WmsInventoryCount wmsInventoryCount)
    {
        List<WmsInventoryCountDetail> wmsInventoryCountDetailList = wmsInventoryCount.getWmsInventoryCountDetailList();
        Long id = wmsInventoryCount.getId();
        if (StringUtils.isNotNull(wmsInventoryCountDetailList))
        {
            List<WmsInventoryCountDetail> list = new ArrayList<WmsInventoryCountDetail>();
            for (WmsInventoryCountDetail wmsInventoryCountDetail : wmsInventoryCountDetailList)
            {
                wmsInventoryCountDetail.setInventoryCountId(id);
                list.add(wmsInventoryCountDetail);
            }
            if (list.size() > 0)
            {
                wmsInventoryCountMapper.batchWmsInventoryCountDetail(list);
            }
        }
    }
}
